﻿using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;

namespace Importer
{
  public interface ITable
  {
    string Name { get; }
    Column[] Columns { get; set; }
    IEnumerable<Row> Rows { get; }
  }
  public abstract class Table<TRow> : ITable
    where TRow : Row
  {
    protected abstract Column[] ColumnDefinitions();
    protected abstract string Dir { get; }
    protected abstract TRow CreateRow(ITable owner);
    public abstract string Name { get; }
    public Column[] Columns { get; set; }
    IEnumerable<Row> ITable.Rows { get { return Rows; } }
    public TRow[] Rows { get; set; }

    protected Table()
    {
      LoadData();
    }

    public void LoadData()
    {
      Columns = ColumnDefinitions();
      foreach (var column in Columns)
        column.LoadData(Dir);
      Debug.Assert(
        Columns.Max(c => c.Values.Length) ==
        Columns.Min(c => c.Values.Length));

      Rows = new TRow[Columns.First().Values.Length];
      for (int i = 0; i < Rows.Length; i++)
      {
        var row = CreateRow(this);
        row.Values = new string[Columns.Length];
        Rows[i] = row;
        for (int j = 0; j < Columns.Length; j++)
        {
          Rows[i].Values[j] = Columns[j].Values[i];
        }
      }
      foreach (var column in Columns)
      {
        column.Values = null;
      }
    }

  }
}